package com.ruoyi.web.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.web.service.FoodIngredientService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.domain.FoodIngredient;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 食材Controller
 *
 * @author xian_pei
 * @date 2024-06-13
 */
@RestController
@RequestMapping("/ingredient/ingredient")
public class FoodIngredientController extends BaseController
{
    @Autowired
    private FoodIngredientService foodIngredientService;

    /**
     * 查询食材列表
     */
    @PreAuthorize("@ss.hasPermi('ingredient:ingredient:list')")
    @GetMapping("/list")
    public TableDataInfo list(FoodIngredient foodIngredient)
    {
        startPage();
        List<FoodIngredient> list = foodIngredientService.selectFoodIngredientList(foodIngredient);
        return getDataTable(list);
    }

    /**
     * 导出食材列表
     */
    @PreAuthorize("@ss.hasPermi('ingredient:ingredient:export')")
    @Log(title = "食材", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FoodIngredient foodIngredient)
    {
        List<FoodIngredient> list = foodIngredientService.selectFoodIngredientList(foodIngredient);
        ExcelUtil<FoodIngredient> util = new ExcelUtil<FoodIngredient>(FoodIngredient.class);
        util.exportExcel(response, list, "食材数据");
    }

    /**
     * 获取食材详细信息
     */
    @PreAuthorize("@ss.hasPermi('ingredient:ingredient:query')")
    @GetMapping(value = "/{ingredientId}")
    public AjaxResult getInfo(@PathVariable("ingredientId") Long ingredientId)
    {
        return success(foodIngredientService.selectFoodIngredientByIngredientId(ingredientId));
    }

    /**
     * 新增食材
     */
    @PreAuthorize("@ss.hasPermi('ingredient:ingredient:add')")
    @Log(title = "食材", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FoodIngredient foodIngredient)
    {
        return toAjax(foodIngredientService.insertFoodIngredient(foodIngredient));
    }

    /**
     * 修改食材
     */
    @PreAuthorize("@ss.hasPermi('ingredient:ingredient:edit')")
    @Log(title = "食材", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FoodIngredient foodIngredient)
    {
        return toAjax(foodIngredientService.updateFoodIngredient(foodIngredient));
    }

    /**
     * 删除食材
     */
    @PreAuthorize("@ss.hasPermi('ingredient:ingredient:remove')")
    @Log(title = "食材", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ingredientIds}")
    public AjaxResult remove(@PathVariable Long[] ingredientIds)
    {
        return toAjax(foodIngredientService.deleteFoodIngredientByIngredientIds(ingredientIds));
    }
}
